import unittest as ut
from ... import DataString as ds
from ..Error import *
from .UT_Base import *



class ErrorTestCase(AcpTestCase):

    def _Parse(self, msg):
        dataStr = ds.CDataString(msg)
        item = CErrorElement()
        item.Parse(dataStr)
        return item


    def test_noError(self):
        subItemName = "Header"

        item = self._Parse("0100")

        sItem = self._GetSubItem(item, subItemName)
        self.assertNotEqual(sItem, item)
        raw = sItem._rawStr
        info = sItem._infoStr

        self.assertEqual(raw, "0x01")

        self._Show(item)

    def test_invalidLength(self):
        subItemName = "Header"

        item = self._Parse("0200")

        sItem = self._GetSubItem(item, subItemName)
        self.assertNotEqual(sItem, item)
        raw = sItem._rawStr
        info = sItem._infoStr
        err = sItem._errStr

        self.assertEqual(err, "Invalid length")
        self._Show(item)

    def test_errOctet(self):
        subItemName = "Error Octet"

        item = self._Parse("0101")

        sItem = self._GetSubItem(item, subItemName)
        self.assertNotEqual(sItem, item)
        raw = sItem._rawStr
        info = sItem._infoStr
        err = sItem._errStr

        self.assertEqual(raw, "1")
        self.assertEqual(info, "Service currentely not available")
        self._Show(item)

    def test_invalidErrOctet(self):
        subItemName = "Error Octet"

        item = self._Parse("0102")

        sItem = self._GetSubItem(item, subItemName)
        self.assertNotEqual(sItem, item)
        raw = sItem._rawStr
        info = sItem._infoStr
        err = sItem._errStr

        self.assertEqual(raw, "2")
        self.assertEqual(err, "Invalid Error Octet")
        self._Show(item)

def suite():
    suite = ut.TestSuite()
    errSuite = ut.TestLoader().loadTestsFromTestCase(ErrorTestCase)
    suite.addTest(errSuite)
    return suite
